﻿using MxWeiXinPF.API.Payment.wxpay;
using MxWeiXinPF.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WxPayAPI;

namespace MxWeiXinPF.Web.api.payment
{
    public partial class paypage : Page
    {
        protected int expireMinute = 0;
        protected string openid = "";
        public string packageValue = "";
        protected int wid = 0;
        private int shopid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            this.openid = MyCommFun.RequestOpenid();
            int id = MyCommFun.RequestInt("orderid");
            this.wid = MyCommFun.RequestInt("wid");
            this.shopid = MyCommFun.RequestInt("shopid");
            this.expireMinute = MyCommFun.RequestInt("expireminute");
            if (this.expireMinute == 0)
            {
                this.expireMinute = 10;
            }
            else if (this.expireMinute == -1)
            {
                this.expireMinute = 0x40290;
            }
            if (((this.openid != "") && (id != 0)) && (this.wid != 0))
            {
                MxWeiXinPF.Model.orders model = new MxWeiXinPF.BLL.orders().GetModel(id, this.wid);
                this.litout_trade_no.Text = model.order_no;
                this.litMoney.Text = model.order_amount.ToString();
                this.litDate.Text = model.add_time.ToString();
                this.litPayType.Text = (model.payment_id.ToString() != "3") ? "门店取货" : "微支付";
                if (model.payment_id == 1)
                {
                    base.Response.Write("<span style='color:#FF0000;font-size:20px'>门店取货订单不用支付，如需变更支付方式请关闭订单重新下单</span>");
                    this.submit.Visible = false;
                }
                else
                {
                    JsApiPay pay = new JsApiPay(this)
                    {
                        openid = this.openid
                    };
                    decimal num2 = model.order_amount * 100M;
                    pay.total_fee = Convert.ToInt32(num2);
                    //pay.out_trade_no = model.order_no;
                    pay.bodystr = "订单支付" + model.id.ToString();
                    pay.attachstr = this.wid + "|" + model.id.ToString();
                    try
                    {

                        WxPayData unifiedOrderResult = pay.GetUnifiedOrderResult();
                        model.trade_no = pay.out_trade_no;
                        new MxWeiXinPF.BLL.orders().Update(model);
                        wxJsApiParam = pay.GetJsApiParameters();
                        Log.Debug(base.GetType().ToString(), "wxJsApiParam : " + wxJsApiParam);
                    }
                    catch (Exception)
                    {
                        base.Response.Write("<span style='color:#FF0000;font-size:20px'>下单失败，请返回重试</span>");
                        this.submit.Visible = false;
                    }
                }
            }
        }

        protected void WxPayData(decimal ttFee, string busiBody, string out_trade_no)
        {
            WxPayHelper helper = new WxPayHelper();
            MxWeiXinPF.Model.wx_payment_wxpay modelByWid = new MxWeiXinPF.BLL.wx_payment_wxpay().GetModelByWid(this.wid);
            string partnerId = modelByWid.partnerId;
            string appId = modelByWid.appId;
            string partnerKey = modelByWid.partnerKey;
            string paySignKey = modelByWid.paySignKey;
            helper.SetAppId(appId);
            helper.SetAppKey(paySignKey);
            helper.SetPartnerKey(partnerKey);
            helper.SetSignType("MD5");
            helper.SetParameter("bank_type", "WX");
            helper.SetParameter("body", busiBody);
            helper.SetParameter("attach", this.wid + "|" + busiBody);
            helper.SetParameter("partner", partnerId);
            helper.SetParameter("out_trade_no", out_trade_no);
            helper.SetParameter("total_fee", ((int)(ttFee * 100M)).ToString());
            helper.SetParameter("fee_type", "1");
            helper.SetParameter("notify_url", "http://" + HttpContext.Current.Request.Url.Authority + "/api/payment/wxpay/notify_url.aspx");
            helper.SetParameter("spbill_create_ip", MXRequest.GetIP());
            helper.SetParameter("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));
            helper.SetParameter("time_expire", DateTime.Now.AddMinutes((double)this.expireMinute).ToString("yyyyMMddHHmmss"));
            helper.SetParameter("input_charset", "UTF-8");
            this.packageValue = helper.CreateBizPackage();
        }

        public static string wxJsApiParam
        {
            get;
            set;
        }
    }
}